package com.powergisol.gis.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FileUtil {
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    public static List<File> recursionFile(String recursionPath, List<File> list) {
        if(null == list){
            list = new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>();
        File file = new File(recursionPath);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file1 : files) {
                    if (file1.isDirectory()) {
                        recursionFile(file1.getAbsolutePath(),list);
                    } else {
                        list.add(file1);
                    }
                }
            }
        }
        return list;
    }

    public static String getContent(String pathname){
        File file = new File(pathname); // 要读取以上路径的output.txt文件
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        if(file.isFile() && file.exists()){
            try {
                fileInputStream = new FileInputStream(file);
                inputStreamReader = new InputStreamReader(fileInputStream);
                bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer sb = new StringBuffer();
                String text = null;
                while((text = bufferedReader.readLine()) != null){
                    sb.append(text);
                }
                bufferedReader.close();
                inputStreamReader.close();
                fileInputStream.close();
                return sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                    if (inputStreamReader != null) {
                        inputStreamReader.close();
                    }
                    if (bufferedReader != null) {
                            bufferedReader.close();
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return null;
    }
    public static String getDatafromFile(File fileName) {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

    public static void main(String[] args) {
        ZipUtil.unZip("E:\\钻孔\\CDMZK2020-8-31(1).zip","E:\\钻孔\\94");
       List<File> listFile = recursionFile("E:\\钻孔\\94",null);
        for (File file : listFile){
            System.out.println(file.getAbsolutePath());
        }
    }
}
